package com.speaktoitcookbook.dao.impl;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import com.speaktoitcookbook.bean.Comment;
import com.speaktoitcookbook.dao.ICommentDao;
import com.speaktoitcookbook.util.DBUtil;
import com.speaktoitcookbook.util.TransformUtil;

/**
 * 用户评论数据操作类
 * 
 * @author 杨强
 * 
 */
public class CommentDaoImpl implements ICommentDao {

    private Connection conn;

    public CommentDaoImpl(Connection conn) {
        this.conn = conn;
    }

    @Override
    public boolean doCreate(Comment comment) throws SQLException {
        boolean result = false;
        String sql = "insert into `tb_comments`(`userID`, `cbID`, `comDate`, `comStars`, `comContent`) values(?, ?, ?, ?, ?)";

        int item = 0;
        Object[] params = new Object[5];
        params[item++] = comment.getUserID();
        params[item++] = comment.getCookbookID();
        params[item++] = new Timestamp(comment.getCommentDate().getTime());
        params[item++] = comment.getStars();
        params[item++] = comment.getContent();

        result = DBUtil.execute(conn, sql, params);
        return result;
    }

    @Override
    public boolean doDelete(long commentID) throws SQLException {
        boolean result = false;
        String sql = "delete from `tb_comments` where `comID` = ?";
        result = DBUtil.execute(conn, sql, commentID);
        return result;
    }

    @Override
    public boolean doUpdate(Comment comment) throws SQLException {
        boolean result = false;
        String sql = "update `tb_comments` set `comID` = ?, `userID` = ?, `cbID` = ?, `comDate` = ?, `comStars` = ?, `comContent` = ? where `comID` = ?";
        Object[] params = TransformUtil.beanTOArray(comment);
        result = DBUtil.execute(conn, sql, params, params[0]);
        return result;
    }

    @Override
    public List<Comment> getComment(String userID) throws SQLException {
        List<Comment> result = null;
        String sql = "select * from `tb_comments` where `userID` = ?";
        List<Object[]> all = DBUtil.query(conn, sql, userID);
        if (all.size() > 0) {
            result = new ArrayList<Comment>(all.size());
            for (Object[] objects : all) {
                Comment comment = new Comment();
                comment = (Comment) TransformUtil.arrayToBean(objects,
                        comment.getClass());
                result.add(comment);
            }
        }
        return result;
    }

    @Override
    public List<Comment> getComment(long cookbookID) throws SQLException {
        List<Comment> result = null;
        String sql = "select * from `tb_comments` where `cbID` = ?";
        List<Object[]> all = DBUtil.query(conn, sql, cookbookID);
        if (all.size() > 0) {
            result = new ArrayList<Comment>(all.size());
            for (Object[] objects : all) {
                Comment comment = new Comment();
                comment = (Comment) TransformUtil.arrayToBean(objects,
                        Comment.class);
                result.add(comment);
            }

        }
        return result;
    }

    @Override
    public List<Comment> getComment(Object... params) throws SQLException {
        List<Comment> result = null;
        StringBuffer sql = new StringBuffer("select * from `tb_comments` ");
        if (null != params && params.length > 0) {
            for (Object param : params) {
                sql.append(param);
            }
        }
        List<Object[]> all = DBUtil.query(conn, sql.toString());
        if (all.size() > 0) {
            result = new ArrayList<Comment>(all.size());
            for (Object[] objects : all) {
                Comment comment = new Comment();

                comment = (Comment) TransformUtil.arrayToBean(objects,
                        Comment.class);

                result.add(comment);
            }
        }
        return result;
    }
}
